__author__ = 'sdjkx'

try:
    from PyQt5.QtGui import *
    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
except ImportError:
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *

class TableShowDialog(QDialog):
    def __init__(self, parent=None,mydb = None):
        super(TableShowDialog, self).__init__(parent)

        self.setWindowTitle("标签数量列表")
        self.mydb = mydb
        self.querySQL = "SELECT b.label_name,sum(IFNULL(label_count,0)) as label_count, 3000 as all_count, 3000 - sum(IFNULL(label_count,0)) as need_count " \
                        "from sys_label b left join u_image_label a on a.label_id=b.id group by b.label_name " \
                        "order by need_count desc,label_count asc, LENGTH(b.label_name), b.label_py "
        self.imageLabelColumn = 4

        self.resize(666, 660)
        self.layout = QVBoxLayout()
        self.centralWidget = QWidget(self)
        self.centralWidget.setObjectName("centralWidget")
        # self.retranslateUi(TableShowDialog)

        self.lbl_name = QLabel()
        # self.lbl_name.setGeometry(QRect(40, 30, 72, 15))
        self.lbl_name.setObjectName("lbl_name")
        self.lbl_name.setText("标签列表：")
        self.layout.addWidget(self.lbl_name)

        self.tableWidget = QTableWidget(self.centralWidget)
        self.tableWidget.setGeometry(QRect(0, 30, 666, 640))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(0)
        self.tableWidget.setRowCount(0)
        # self.tableWidget.setStyleSheet("selection-background-color:pink")
        # self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.tableWidget.raise_()

        self.creat_table_show()

    def creat_table_show(self):
        ###===========读取表格，转换表格，============================================
        ###======================给tablewidget设置行列表头============================

        records = self.mydb.executeSql(self.querySQL)

        self.tableWidget.setColumnCount(self.imageLabelColumn)
        self.tableWidget.setRowCount(len(records))
        horizontalHeader = ["标签名称","已标注数量","需求数量","还要标注数量"]
        self.tableWidget.setHorizontalHeaderLabels(horizontalHeader)

        ###======================给tablewidget设置行列表头============================

        ###================遍历表格每个元素，同时添加到tablewidget中========================
        i = -1
        for row in records:
            i = i + 1
            for j in range(4):
                text = str(row[j])
                newItem = QTableWidgetItem(text)
                if j == 0:
                    newItem.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter)
                else:
                    newItem.setTextAlignment(Qt.AlignRight|Qt.AlignVCenter)
                self.tableWidget.setItem(i, j, newItem)

    @staticmethod
    def show(parent=None, mydb = None):
        dialog = TableShowDialog(parent, mydb)
        dialog.exec_()